package com.ijiuchuang.jc_localization.view.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.PopupMenu;
import android.view.MenuItem;
import android.view.View;

import com.ijiuchuang.jc_localization.R;
import com.ijiuchuang.jc_localization.bean.entity.BleDevice;
import com.ijiuchuang.jc_localization.tools.adapter.ViewHolder;
import com.ijiuchuang.jc_localization.tools.adapter.recyclerview.CommonAdapter;
import com.ijiuchuang.jc_localization.view.activity.ToolsConnectionActivity;
import com.ijiuchuang.jc_localization.view.activity.ToolsGuardsActivity;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Veev on 2016/6/13
 *
 * @link 384506557@qq.com
 */
public class ToolsRssiRecyclerAdapter extends CommonAdapter<BleDevice> {

    public ToolsRssiRecyclerAdapter(Context context, int layoutId, List<BleDevice> datas) {
        super(context, layoutId, datas);
    }

    DecimalFormat df = new DecimalFormat("###.000");

    @Override
    public void convert(final ViewHolder holder, final BleDevice bleDevice) {
        holder.setText(R.id.recycler_item_tools_rssi_text_name, bleDevice.getName());
        holder.setText(R.id.recycler_item_tools_rssi_text_mac, bleDevice.getMac());
        holder.setText(R.id.recycler_item_tools_rssi_text_rssi_origin, "原始Rssi: " + bleDevice.getRssi());
        holder.setText(R.id.recycler_item_tools_rssi_text_rssi_filter, "滤波Rssi: " + df.format(bleDevice.getPreRssi()));
        holder.setText(R.id.recycler_item_tools_rssi_text_rssi_broadcast_data, "广播: " + bleDevice.getBcData());
        holder.setOnClickListener(R.id.recycler_item_tools_rssi_button_menu, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //参数View 是设置当前菜单显示的相对于View组件位置，具体位置系统会处理
                PopupMenu popupMenu = new PopupMenu(mContext, holder.getView(R.id.recycler_item_tools_rssi_button_menu));
                //加载menu布局
                popupMenu.getMenuInflater().inflate(R.menu.menu_tools_rssi, popupMenu.getMenu());
                //设置menu中的item点击事件
                popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        int id = item.getItemId();
                        switch (id) {
                            case R.id.menu_tools_rssi_guards:
                                Intent intentGuards = new Intent(mContext, ToolsGuardsActivity.class);
                                Bundle bundleGuards = new Bundle();
                                bundleGuards.putSerializable(ToolsGuardsActivity.TOOLS_GUARDS_DEVICE, bleDevice);
                                intentGuards.putExtras(bundleGuards);
                                mContext.startActivity(intentGuards);
                                break;
                            case R.id.menu_tools_rssi_watch:
                                break;
                            case R.id.menu_tools_rssi_connect:
                                Intent intentConnect = new Intent(mContext, ToolsConnectionActivity.class);
                                Bundle bundleConnect = new Bundle();
                                bundleConnect.putSerializable(ToolsConnectionActivity.TOOLS_CONNECTION_DEVICE, bleDevice);
                                intentConnect.putExtras(bundleConnect);
                                mContext.startActivity(intentConnect);
                                break;
                        }
                        return false;
                    }
                });
                //设置popupWindow消失的点击事件
                popupMenu.setOnDismissListener(new PopupMenu.OnDismissListener() {
                    @Override
                    public void onDismiss(PopupMenu menu) {

                    }
                });
                popupMenu.show();
            }
        });
    }
}
